var config = require("config");

cc.Class({
    extends: cc.Component,

    properties: {
        bg: cc.Node,
        display: cc.Sprite,
    },

    onLoad () {
        var height = cc.view.getVisibleSize().height;
        this.node.height = height;
        this.bg.height = height/2*1.3;
        config.loadImage(this.bg,"pet_bg","resources/image/");

        this.node.on(cc.Node.EventType.TOUCH_START, this.onBtnTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onBtnTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onBtnTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onBtnTouchCancel, this);
    },

    start () {
        var UserData = config.UserData;
        if(typeof(wx) != "undefined"){
            var openDataContext = wx.getOpenDataContext();
            var sharedCanvas = openDataContext.canvas;
            if(UserData.subWidth == 0){
                UserData.subWidth = sharedCanvas.width; //子域的宽
            }
            if(UserData.subHeight == 0){
                UserData.subHeight = sharedCanvas.height; //子域的高
            }
            if(sharedCanvas){
                sharedCanvas.width = 1080;
                sharedCanvas.height = 1920;
            }
            // 发消息给子域
            var data = JSON.stringify(UserData);
            wx.postMessage({
                message: {
                    key: 'LoginRank',
                    value: data
                }
            })
        }

        var callback = function () {
            //初始化结算排行版显示纹理
            this._tex = new cc.Texture2D();
        }
        this.scheduleOnce(callback ,0.2);
    },
    update (dt) {
        this._updateSubDomainCanvas();
    },
    // 刷新子域的纹理
    _updateSubDomainCanvas () {
        if (!this._tex) {
            return;
        }

        if(typeof(wx) != "undefined"){
            var openDataContext = wx.getOpenDataContext();
            var sharedCanvas = openDataContext.canvas;
            this._tex.initWithElement(sharedCanvas);
            this._tex.handleLoadedTexture();
            this.display.spriteFrame = new cc.SpriteFrame(this._tex);
        }
    },

    //返回
    onBtnBack:function(){
        cc.vv.audioMgr.playSFX("ui_button");
        this.node.destroy();
    },

    //按下时事件
    onBtnTouchStart:function(event){
        event.stopPropagation();
    },

    //按住移动后事件
    onBtnTouchMove:function(event){
        event.stopPropagation();
    },

    //按下后松开后事件
    onBtnTouchEnd:function(event){
        event.stopPropagation();
    },

    //按下取消事件
    onBtnTouchCancel:function(event){
        event.stopPropagation();
    },
});
